//
//  MessageVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/22.
//  Copyright © 2020 tym. All rights reserved.
//

#import "MessageVC.h"
#import "MessageSettingVC.h"
#import "MessageCell.h"
#import "MessageContentVC.h"
#import "FamilyModel.h"
#import "UserModel.h"
#import "UserMessageService.h"
#import "TYMBaseTableView.h"
@interface MessageVC ()

@property(nonatomic,weak) IBOutlet TYMBaseTableView *tableView;

@property(nonatomic,strong) UserMessageService *messageService;
@property(nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation MessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"消息"];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageCell class]) bundle:nil] forCellReuseIdentifier:[MessageCell identifier]];
    
    [self addRightBtnImage:@"icon_home8"];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getMessageData];
}

-(void)rightFunc:(id)btn{
    MessageSettingVC *messageVC = [[MessageSettingVC alloc] init];
    [self.navigationController pushViewController:messageVC animated:YES];
}

-(void)getMessageData{
    TYMWeak(self);
    [self.messageService getFindUsermessagesFamilyID:[UserTool getCurrentFamily].familyId completion:^(LMJBaseResponse *respon, NSArray<UserMessageModel *> *mesageArray) {
        if (weakself.dataArray.count) {
            [weakself.dataArray removeAllObjects];
        }
        [weakself.dataArray addObjectsFromArray:mesageArray];
        [weakself.tableView reloadData];
    }];
}

-(UserMessageService*)messageService{
    if (_messageService == nil) {
        _messageService = [[UserMessageService alloc] init];
    }
    return _messageService;
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

//MARK: - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   return [MessageCell cellForHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:[MessageCell identifier]];
    if(self.dataArray.count){
       cell.messageModel = self.dataArray[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MessageContentVC *contentVC = [[MessageContentVC alloc] initWithMessageModel:self.dataArray[indexPath.row]];
    [self.navigationController pushViewController:contentVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
